#
# EncoderView.py
#
# This work is released under the GNU GPL, version 2 or later.
#
from LibraryView import *
from PlaylistView import *
from EncoderViewItem import *
from EncoderTrackDrag import *
from DB.EncoderLibrary import *

class EncoderView(PlaylistView):
	def __init__(self,container):
		PlaylistView.__init__(self,container)
		self.library = EncoderLibrary.singleton()
		self.setPlaylist(self.library.playlist)
	
	def setCurrentPlaylist(self):
		pass

	def loadHeaders(self):
		headers = ["","",i18n("Song Name"),i18n("Time"),i18n("Artist"),i18n("Album"),i18n("Genre")]
		widths = [20,20,220,40,120,180,80]
		for index in xrange(0,len(headers)):
			self.addColumn(headers[index],widths[index])
		self.setShowSortIndicator(True)
		self.setFullWidth(True)
		#self.setColumnAlignment(0,Qt.AlignRight)
		self.ratingColumn = 99
		sortColumn = self.settings.get('Encoder Sort Column',0)
		sortAscending = self.settings.get('Encoder Sort Ascending',True)
		self.setSorting(sortColumn,sortAscending)
		QObject.connect(self.header(),SIGNAL("indexChange(int,int,int)"),self.columnIndexChanged)
		QObject.connect(self.header(),SIGNAL("sizeChange(int,int,int)"),self.columnSizeChanged)
		self.loadHeaderSizes('Encoder Header Widths')
		self.loadHeaderOrdering('Encoder Header Map')

	def loadTracks(self):
		pass

	def setPlaylist(self,playlist):
		self.playlistName = playlist.name
		self.playlist = playlist
		self.playlist.connect(PYSIGNAL("addedTrack"),self.refresh)
		self.playlist.connect(PYSIGNAL("removedTracks"),self.refresh)
		self.loadPlaylistItems()

	def loadPlaylistItems(self):
		self.clear()
		index = 1
		tracks = self.playlist.tracks
		for track in tracks:
			self.loadTrackItemForIndex(track,index)
			index = index+1
		self.emitCurrentEnabledSortedTracks()

	def loadTrackItemForIndex(self,track,index):
		item = LEncoderViewItem(self,track,index)

	def canBurn(self):
		return False
	
	def canPlay(self):
		return False

	def canShowLocation(self):
		return False
	
	def canGetInfo(self):
		return False

	def refresh(self):
		self.loadPlaylistItems()

	def doSelectionChanged(self):
		LLibraryView.doSelectionChanged(self)
		self.emit(PYSIGNAL("selectionChanged"),(self,None))

	def setSorting(self,column,ascending):
		KListView.setSorting(self,column,ascending)
		self.settings['Encoder Sort Column'] = column
		self.settings['Encoder Sort Ascending'] = not not ascending

	def columnIndexChanged(self,section,fromIndex,toIndex):
		self.saveHeaderOrdering('Encoder Header Map')
	
	def columnSizeChanged(self,section,oldSize,newSize):
		self.saveHeaderSizes('Encoder Header Widths')

	def removeSelectedItems(self):
		trackIDs = self.selectedTrackIDs()
		self.removeTrackIDsFromAllPlaylists(trackIDs)
		self.killTrackIDs(trackIDs)

	def contextMenu(self,item,pos,col):
		pass
	
	def contentsDragEnterEvent(self,event):
		pass
	
	def contentsDropEvent(self,event):
		pass

	def acceptDrag(self,event):
		return False

	def dragObject(self):
		#print "dragging remote tracks"
		selectedTrackIDs = self.selectedTrackIDs()
		if selectedTrackIDs and len(selectedTrackIDs)>0:
			drag = LEncoderTrackDrag(selectedTrackIDs,self)
			drag.dragCopy()
